﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DepartmentSystem
{
    public partial class WorkerAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string workerName = TextBox1.Text;
            string strAge = TextBox2.Text;
            string strWage = TextBox3.Text;
            string deptNum = TextBox4.Text;
            string phoneNum = TextBox5.Text;
            string office = TextBox6.Text;

            try
            {
                if (strAge.Length != 0)
                {
                    int age = Int16.Parse(strAge);
                    if (age < 18|| age > 80)
                    {
                        Response.Write("<script>alert('年龄范围 18 - 80!')</script>");
                        return;
                    }
                }
            }
            catch (Exception)
            {
                Response.Write("<script>alert('年龄无效!')</script>"); return;
            }

            try
            {
                if (strWage.Length != 0)
                {
                    int wage = Int16.Parse(strWage);
                    if (wage < 800 || wage > 4500)
                    {
                        Response.Write("<script>alert('工资范围 800 - 4500!')</script>");
                        return;
                    }
                }
                else
                {
                    Response.Write("<script>alert('工资不能为空!')</script>"); return;
                }
            }
            catch (Exception)
            {
                Response.Write("<script>alert('工资无效!')</script>"); return;
            }

            string sql = "insert into tb_worker (workerName, Age, Wage, deptNum, phoneNum, office) " +
                "values('" + workerName + "', '" + strAge + "', '" + strWage + "', '" + deptNum + "', '" + phoneNum + "', '"+ office + "')";

            if (SQLHelper.executeNoQuery(sql) != 1)
            {
                Response.Write("<script>alert('添加失败!')</script>");
            }
            else
            {
                Response.Write("<script>alert('添加成功!')</script>");
                Server.Transfer("WorkerInfo.aspx");

            }
        }
    }
}